//
//  RulesDetailViewController.m
//  carcareIOS
//
//  Created by wr on 15/7/19.
//  Copyright (c) 2015年 baozun. All rights reserved.
//

#import "RulesDetailViewController.h"
#import "RuleDetailCell.h"

//#define TABLEVIEW_HEIGHT (320)

#define CELL_HEIGHT_NORMALL (45)

#define CELL_HEIGHT_ONE (85)

@interface RulesDetailViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic , strong) UITableView * tableView;

@property (nonatomic , readonly) NSArray * titleArray;

@property (nonatomic , strong) NSDictionary * dataDic;

@property (nonatomic , copy) NSString * queryIDStr;

@property (nonatomic , strong) ViolationMgr * cityMgr;

@property (nonatomic , strong) ViolationCarModel * carModel;

@property (nonatomic , assign) CGFloat oneCellHeight;

@property (nonatomic , assign) CGFloat zeroCellHeight;

@end

@implementation RulesDetailViewController
- (instancetype)initWithData:(NSDictionary *)dataDic withQuryID:(NSString *)queryID withDataManager:(ViolationMgr *)cityMgr withCarModel:(ViolationCarModel *)carModel
{
    self = [super init];
    if (self) {
        self.dataDic = [NSDictionary dictionaryWithDictionary:dataDic];
        self.queryIDStr = queryID;
        self.cityMgr = cityMgr;
        self.carModel = carModel;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"违章详情";
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, __MAIN_WIDTH, __MAIN_HEIGHT_64) style:UITableViewStylePlain];
    self.tableView.backgroundColor = [Theme backGroundColor];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.bounces = NO;
    self.tableView.separatorColor = [UIColor clearColor];
    self.tableView.tableFooterView = [[UIView alloc] init];
    [self.view addSubview:self.tableView];

}
- (void)viewWillDisappear:(BOOL)animated
{
    [self.delegate configDetailWithQueryID:self.queryIDStr];
}
-(NSArray *)titleArray
{
    return @[@"违章地点:",@"违章内容:",@"违章状态:",@"违章时间:",@"违章罚款:",@"违章扣分:"];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 100;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView * footerView = [Factory viewWithFrame:CGRectMake(0, 0, __MAIN_WIDTH, 100) bgColor:[Theme backGroundColor] onView:nil];
    __weak __typeof(self) wself = self;
    [ViewFactory normalButtonWithFrame:FactoryRectButtonDefault(20) title:@"马上处理" inView:footerView action:^{
        
        [wself.cityMgr payLinkWithCarInfo:self.carModel.cxPayNeedInfos result:^(NSString *url) {
            [UmengMgr event:ICZ_CheckCarTraffic_PayService];

            BaseWebViewController * webVC = [[[BaseWebViewController alloc]initWithURL:url]setupNavBack];

            webVC.title = @"违章缴费";
            webVC.isScaleBool = YES;
            [wself.navigationController pushViewController:webVC animated:YES];
        }];
    }];
    return footerView;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.titleArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 5 ;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView * view = [Factory viewWithFrame:CGRectMake(0, 0, __MAIN_WIDTH, 5) bgColor:[UIColor whiteColor] onView:nil];
    return view;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString * oneDetailStr = self.dataDic[@"detail"];
    NSDictionary *attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:16]};
    CGSize labelSize = [oneDetailStr boundingRectWithSize:CGSizeMake(__MAIN_WIDTH - 105 - 22 * __MAIN_RATIO_375 , CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size;
    
    NSString * zeroDetailStr = self.dataDic[@"address"];
    CGSize zeroLabelSize = [zeroDetailStr boundingRectWithSize:CGSizeMake(__MAIN_WIDTH - 105 - 22 * __MAIN_RATIO_375 , CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size;
    self.oneCellHeight = ((labelSize.height + 22 * __MAIN_RATIO_375 * 2 ) > CELL_HEIGHT_ONE ? (labelSize.height + 22 * __MAIN_RATIO_375 * 2 ) : CELL_HEIGHT_ONE);
    self.zeroCellHeight = ((zeroLabelSize.height + 22 * __MAIN_RATIO_375 * 2 ) > CELL_HEIGHT_NORMALL ? (zeroLabelSize.height + 22 * __MAIN_RATIO_375 * 2 ) : CELL_HEIGHT_NORMALL);
    return (indexPath.row != 1 ? (indexPath.row == 0 ? self.zeroCellHeight : CELL_HEIGHT_NORMALL) : self.oneCellHeight);
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * cellID = @"cellID";
    RuleDetailCell * cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[RuleDetailCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    NSString * detailStr ;
    switch (indexPath.row) {
        case 0:
            detailStr = self.dataDic[@"address"];
            break;
        case 1:
            detailStr = self.dataDic[@"detail"];
            break;
        case 2:
            detailStr = @"未处理";
            break;
        case 3:
            detailStr = self.dataDic[@"dateString"];
            break;
        case 4:
            detailStr = ([[NSString stringWithFormat:@"%@",self.dataDic[@"money"]] isNotNil] ? [NSString stringWithFormat:@"%@元",self.dataDic[@"money"]] : @"未知");
            break;
        case 5:
            detailStr = ([[NSString stringWithFormat:@"%@",self.dataDic[@"point"]] isNotNil] ? [NSString stringWithFormat:@"%@分",self.dataDic[@"point"]] : @"未知");
            break;
        default:
            break;
    }
    [cell configdataTitle:self.titleArray[indexPath.row] dataDetailStr:detailStr withIndex:indexPath.row withCellHeight:(indexPath.row != 1 ?  (indexPath.row == 0 ? self.zeroCellHeight : CELL_HEIGHT_NORMALL) : self.oneCellHeight )];
    return cell;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
